# 1-导包
from bs4 import BeautifulSoup


# 2-定义一个bs提取工具类方法
def html_parser(html):

    # 2.1-实例化bs对象, html.parser代表的是要处理的文件是html格式
    bs = BeautifulSoup(html, "html.parser")

    # 2.2-获取form标签的action属性
    url = bs.form.get("action")

    # 2.3-获取所有的input标签对象组成的列表
    imput_list = bs.find_all("input")

    # 初始化一个请求参数字典
    data = {}

    # 2.4-遍历input标签组成的字典，将name和value值添加到data里面
    for input in imput_list:
        data[input.get("name")] = input.get("value")  # input标签的name作为字典的名称，value作为字典的值

    # 3-将三方请求的url和请求参数返回
    return url, data


if __name__ == "__main__":
    html = "<form name='easypaysubmit' id='easypaysubmit' target='_blank' method='post' action='http:\/\/121.43.169.97:8000\/muser\/publicRequests'>" \
    "<input name='Version' type='hidden' value='10'\/>" \
    "<input name='CmdId' type='hidden' value='UserRegister'\/>" \
    "<input name='MerCustId' type='hidden' value='6000060007313892'\/>" \
    "<input name='BgRetUrl' type='hidden' value='https:\/\/www.baidu.com\/'\/>" \
    "<input name='RetUrl' type='hidden' value='http:\/\/user-p2p-test.itheima.net\/trust\/chinapnr\/register\/return\/22082322272333301212'\/>" \
    "<input name='UsrId' type='hidden' value=''\/>" \
    "<input name='UsrName' type='hidden' value=''\/>" \
    "<input name='IdType' type='hidden' value='00'\/>" \
    "<input name='IdNo' type='hidden' value='330102199003078870'\/>" \
    "<input name='UsrMp' type='hidden' value='13800000004'\/>" \
    "<input name='UsrEmail' type='hidden' value=''\/>" \
    "<input name='MerPriv' type='hidden' value='22082322272333301212'\/>" \
    "<input name='ChkValue' type='hidden' value='-101'\/>" \
    "<input name='CharSet' type='hidden' value='UTF-8'\/>" \
    "<\/form>" \
    "<script>document.forms['easypaysubmit'].submit();<\/script>"

    res = html_parser(html)

    print("获取的html测试结果：{}".format(res))
